package com.firefightingiot.firefightingiot.web.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.firefightingiot.firefightingiot.common.base.BaseService;
import com.firefightingiot.firefightingiot.model.DeviceBusiness;
import com.firefightingiot.firefightingiot.model.ItemSetup;
import com.firefightingiot.firefightingiot.model.ItemSetupDefault;
import com.firefightingiot.firefightingiot.support.model.RespEntity;
import com.firefightingiot.firefightingiot.support.mybatisplus.QueryWrapper;
import com.firefightingiot.firefightingiot.support.utils.MyLogger;
import com.firefightingiot.firefightingiot.support.utils.requestutil.HttpRequestUtil;
import com.firefightingiot.firefightingiot.web.mapper.DeviceBusinessMapper;
import com.firefightingiot.firefightingiot.web.mapper.ItemSetupDefaultMapper;
import com.firefightingiot.firefightingiot.web.mapper.ItemSetupMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 检测项服务
 */
@Service
public class ItemSetupService extends BaseService {

    @Resource
    public MyLogger logger;

    @Resource
    private ItemSetupMapper itemSetupMapper;
    @Resource
    private ItemSetupDefaultMapper itemSetupDefaultMapper;
    @Resource
    private DeviceBusinessMapper deviceBusinessMapper;

    /**
     * 监测项列表
     * @return
     */
    public RespEntity getItemSetupList(JSONObject jsonParam){
        //处理支持form-data和json两种方式
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        List<ItemSetup> itemSetupList = itemSetupMapper.getItemSetupList(jsonParam);

        if (itemSetupList.isEmpty()){
            //查询产品id
            DeviceBusiness deviceBusiness = deviceBusinessMapper.selectOne(new QueryWrapper<DeviceBusiness>().eq("number",jsonParam.get("number")));
            if (deviceBusiness!=null){
                //查询检测项默认值
                List<ItemSetupDefault> itemSetupDefaultList = itemSetupDefaultMapper.selectList(new QueryWrapper<ItemSetupDefault>().eq("product_id",deviceBusiness.getProductId()));
                return RespEntity.success(itemSetupDefaultList);
            }else {
                return RespEntity.success();
            }
        }
        return RespEntity.success(itemSetupList);
    }

    /**
     * 监测项批量插入
     * @param jsonParam
     * @return
     */
    public RespEntity insertItemSetup(JSONObject jsonParam){
        //处理支持form-data和json两种方式
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        List<ItemSetup> itemSetupList = new ArrayList<>();
        JSONArray jsonArray = jsonParam.getJSONArray("item");
        List<ItemSetup> list = itemSetupMapper.selectList(new QueryWrapper<ItemSetup>().eq("number",jsonParam.getStr("number")));
        if (!list.isEmpty()){
                itemSetupMapper.delete(new QueryWrapper<ItemSetup>().eq("number",jsonParam.getStr("number")));
        }
        //遍历jsonArray
        for (Object object : jsonArray){
            JSONObject json = (JSONObject)object;
            ItemSetup itemSetup = new ItemSetup();
            itemSetup.setItem(json.getStr("item"));
            itemSetup.setMax(json.getDouble("max"));
            itemSetup.setMin(json.getDouble("min"));
            itemSetup.setUnit(json.getStr("unit"));
            itemSetup.setNumber(jsonParam.getStr("number"));
            itemSetupList.add(itemSetup);
        }
        itemSetupMapper.insertZB(itemSetupList);
        return RespEntity.success();
    }
}
